package com.library.controller.user;

import com.library.dto.BookPageQueryDTO;
import com.library.dto.BookTagDTO;
import com.library.dto.PageQueryDTO;
import com.library.dto.SearchPageDTO;
import com.library.entity.BookTag;
import com.library.result.PageResult;
import com.library.result.Result;
import com.library.service.BookService;
import com.library.vo.BookTagVO;
import com.library.vo.BookVO;
import com.library.vo.RecommendationsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user/book")
@Slf4j
@Api(tags = "用户书籍相关接口")
public class UserBookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/page")
    @ApiOperation("书籍分页查询")
    public Result<PageResult> page(BookPageQueryDTO bookPageQueryDTO){
        log.info("分页查询：{}", bookPageQueryDTO);

        //如果redis中存在，则从redis中获取
        if(redisTemplate.opsForValue().get("book_page_query_" + bookPageQueryDTO) != null){
            return Result.success((PageResult) redisTemplate.opsForValue().get("book_page_query_" + bookPageQueryDTO));
        }
        PageResult pageResult = bookService.pageQuery(bookPageQueryDTO);
        //将查询结果存入redis中
        redisTemplate.opsForValue().set("book_page_query_" + bookPageQueryDTO, pageResult);
        return Result.success(pageResult);
    }

    /**
     * 根据id查询书籍
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("书籍详情")
    public Result<BookVO> getBookDetailedById(@PathVariable Long id){
        log.info("书籍详情：{}", id);
        BookVO bookVO = bookService.getBookDetailedById(id);
        return Result.success(bookVO);
    }

    /**
     * 搜索
     * @param searchPageDTO
     * @return
     */
    @GetMapping("/search")
    @ApiOperation("搜索")
    public Result<PageResult> search(SearchPageDTO searchPageDTO){
        log.info("搜索：{}", searchPageDTO);
        PageResult pageResult = bookService.searchPageQuery(searchPageDTO);
        return Result.success(pageResult);
    }

    /**
     * 智能推荐
     * @param userId
     * @return
     */
    @GetMapping("/recommendations")
    @ApiOperation("智能推荐")
    public Result<List<RecommendationsVO>> getRecommendations(Long userId){
        log.info("智能推荐：{}", userId);
        List<RecommendationsVO> recommendationsVOList = bookService.getRecommendations(userId);
        return Result.success(recommendationsVOList);
    }

    /**
     * 添加标签
     * @param bookId
     * @param bookTagDTO
     * @return
     */
    @PostMapping("/tags/{bookId}")
    @ApiOperation("添加标签")
    public Result<String> addTag(@PathVariable Long bookId, @RequestBody BookTagDTO bookTagDTO){
        log.info("添加标签：{},{}", bookId, bookTagDTO);
        bookService.addTag(bookId, bookTagDTO);
        return Result.success();
    }

    /**
     * 修改标签
     * @param tagId
     * @param bookTagDTO
     * @return
     */
    @PutMapping("/tags/{tagId}")
    @ApiOperation("修改标签")
    public Result<String> updateTag(@PathVariable Long tagId, @RequestBody BookTagDTO bookTagDTO){
        log.info("修改标签：{}", tagId);
        bookService.updateTag(tagId, bookTagDTO);
        return Result.success();
    }

    /**
     * 获取图书的标签列表
     * @param bookId
     * @return
     */
    @GetMapping("/tags/{bookId}")
    @ApiOperation("获取图书的标签列表")
    public Result<List<BookTagVO>> getTags(@PathVariable Long bookId){
        log.info("获取标签：{}", bookId);
        List<BookTagVO> bookTags = bookService.getTags(bookId);
        return Result.success(bookTags);
    }

    @GetMapping("/tags/search")
    @ApiOperation("搜索标签")
    public Result<PageResult> searchTags(PageQueryDTO pageQueryDTO,@RequestParam String keyword){
        log.info("搜索标签：{}", keyword);
        PageResult pageResult = bookService.searchTags(pageQueryDTO, keyword);
        return Result.success(pageResult);
    }

    @GetMapping("/tags/{tagId}/book")
    @ApiOperation("根据标签获取图书详细")
    public Result<BookVO> getBooksByTagId(@PathVariable Long tagId){
        log.info("根据标签获取图书详细：{}", tagId);
        BookVO bookVO = bookService.getBooksByTagId(tagId);
        return Result.success(bookVO);
    }

    @DeleteMapping("/{bookId}/tags/{tagId}")
    @ApiOperation("删除标签")
    public Result<String> deleteTag(@PathVariable Long bookId, @PathVariable Long tagId){
        log.info("删除标签：{}", tagId);
        bookService.deleteTag(bookId, tagId);
        return Result.success();
    }

}
